package gui;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import particle_simulator_chem.Boid;
import particle_simulator_chem.World;

public class SaveWorld extends Dialog {

	World world;
	
	protected Object result;
	protected Shell shell;
	private Text text;
	private Text text_1;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public SaveWorld(Shell parent, int style, World world) {
		super(parent, style);
		setText("SWT Dialog");
		this.world = world;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(450, 163);
		shell.setText(getText());
		
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setBounds(10, 10, 424, 110);
		composite.setLayout(null);
		
		Composite composite_1 = new Composite(composite, SWT.BORDER);
		composite_1.setLayout(null);
		composite_1.setBounds(0, 0, 424, 110);
		
		Label lblWorldSave = new Label(composite_1, SWT.NONE);
		lblWorldSave.setText("World Save");
		lblWorldSave.setBounds(10, 0, 400, 15);
		
		Label label_1 = new Label(composite_1, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_1.setBounds(0, 21, 420, 2);
		
		Label lblFileName = new Label(composite_1, SWT.NONE);
		lblFileName.setText("File Name (\"world\")\r\n");
		lblFileName.setBounds(10, 30, 137, 15);
		
		Label lblFilePath = new Label(composite_1, SWT.NONE);
		lblFilePath.setText("File Path (\"C:\\Users\\\")\r\n");
		lblFilePath.setBounds(10, 51, 117, 15);
		
		text = new Text(composite_1, SWT.BORDER);
		text.setText("");
		text.setBounds(176, 27, 234, 18);
		
		text_1 = new Text(composite_1, SWT.BORDER);
		text_1.setText("");
		text_1.setBounds(176, 48, 234, 18);
		
		Button btnSaveWorld = new Button(composite_1, SWT.NONE);
		btnSaveWorld.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//progressBar.setUnknown(false);
				File world_file = new File(text_1.getText().concat(text.getText()).concat(".txt"));
				if (!world_file.exists()){
					try {
						world_file.createNewFile();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
				try {
					PrintStream out = new PrintStream(world_file);
					//System.out.println(world.getLeftBound());
					out.println(world.getWidth()); 
					out.println(world.getHeight());
					out.println(world.getGravity());
					out.println(world.getGravX());
					out.println(world.getGravY());
					out.println(world.getDefaultGravity());
					out.println(world.getDefaultGX());
					out.println(world.getDefaultGY());
					out.println(world.getAirFriction());
					
					for(Boid boid : world.getBoids()){
						out.println(boid.getX());
						out.println(boid.getY());
						out.println(boid.getXVel());
						out.println(boid.getYVel());
						out.println(boid.getRawFormula());
					}
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				}
			}
		});
		btnSaveWorld.setBounds(176, 72, 75, 25);
		btnSaveWorld.setText("Save World\r\n");
	}
}
